import requests
from datetime import datetime
import pandas as pd

cookies = {
    'JSESSIONID': '10D9FE3AC687E52992244E4D89C3F10C',
    'tempTokenKey': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLmnY7ogaoiLCJhdWQiOiIxRDFEMTEyQTUwQzE0NEE1QTA3NDFFNTZFNTRCQzlGMCIsInNlcnZlciI6Imh0dHA6Ly81OS4yMTUuMjA2LjE4OC9sb2dpbi8iLCJyb2xlIjoiUk9MRV9BUkVBIiwiaXNJbml0aWFsUGFzc3dvcmQiOiIwIiwicmVnaW9uTmFtZSI6Iui0teW3nuecgSIsImluZHVzdHJ5IjpudWxsLCJhcHBDb2RlIjoiZGdvdi1wb3J0YWwiLCJvcmdhbk5hbWUiOiLmlrDov5vkurrlkZgiLCJyZWdpb25Db2RlIjoiNTIwMDAwIiwiaW5kdXN0cnlMaXN0IjpudWxsLCJvcmdhbkNvZGUiOiI1MjAwMDAyMDE3MDIxNjEwMDUzMDAwMDUwNyIsImV4cGlyYXRpb25UaW1lIjo4NiwiZ3JhZGUiOiIyIiwic3RhdHVzIjoiMSIsImlzcyI6Imluc3B1ciIsImlhdCI6MTc1ODg3OTU0NSwiZXhwIjoxNzU4OTA4MzQ1fQ.cs12PtekDO_rIV0-qn7mKdelzwJ3vnNW8Z_FzjTw98RDCEu1Q4gWil-_ye2ZQPKdi3NmH6VftZBTQ2IicgLb6g',
    'userInfoKey': '{%22role%22:%22ROLE_AREA%22%2C%22isInitialPassword%22:%220%22%2C%22regionName%22:%22%E8%B4%B5%E5%B7%9E%E7%9C%81%22%2C%22appCode%22:%22dgov-portal%22%2C%22organName%22:%22%E6%96%B0%E8%BF%9B%E4%BA%BA%E5%91%98%22%2C%22regionCode%22:%22520000%22%2C%22phone%22:%2204b6bf9c6e9dd2555b6a4771cdc3e7ba%22%2C%22organCode%22:%2252000020170216100530000507%22%2C%22expirationTime%22:86%2C%22name%22:%226e7c1d2bcac5e280ca91a05b5b60cd18%22%2C%22id%22:%221D1D112A50C144A5A0741E56E54BC9F0%22%2C%22email%22:%22d07485450a4b010167a8407b3a713857%22%2C%22username%22:%22187c7c36c1a80ae5bb7963b17986f6d7%22%2C%22status%22:%221%22%2C%22isWeakPwd%22:%220%22}',
    'pwdState': 'true',
    'inspurRoleKey': 'ROLE_AREA',
    'windowId': '0B7CA12E92004E0ABCA9EB88F0A792E4',
    'James_cookie_sYQDUGqqzHtheme': 'custom',
    'SameSite': 'None',
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    # 'Accept-Encoding': 'gzip, deflate',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest',
    'Origin': 'http://59.215.206.188',
    'Connection': 'keep-alive',
    'Referer': 'http://59.215.206.188/tysp/approval/worktodolist/enterBusinessQuery?app_code=INSPUR-DZZW-ACCEPT',
    # 'Cookie': 'JSESSIONID=10D9FE3AC687E52992244E4D89C3F10C; tempTokenKey=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLmnY7ogaoiLCJhdWQiOiIxRDFEMTEyQTUwQzE0NEE1QTA3NDFFNTZFNTRCQzlGMCIsInNlcnZlciI6Imh0dHA6Ly81OS4yMTUuMjA2LjE4OC9sb2dpbi8iLCJyb2xlIjoiUk9MRV9BUkVBIiwiaXNJbml0aWFsUGFzc3dvcmQiOiIwIiwicmVnaW9uTmFtZSI6Iui0teW3nuecgSIsImluZHVzdHJ5IjpudWxsLCJhcHBDb2RlIjoiZGdvdi1wb3J0YWwiLCJvcmdhbk5hbWUiOiLmlrDov5vkurrlkZgiLCJyZWdpb25Db2RlIjoiNTIwMDAwIiwiaW5kdXN0cnlMaXN0IjpudWxsLCJvcmdhbkNvZGUiOiI1MjAwMDAyMDE3MDIxNjEwMDUzMDAwMDUwNyIsImV4cGlyYXRpb25UaW1lIjo4NiwiZ3JhZGUiOiIyIiwic3RhdHVzIjoiMSIsImlzcyI6Imluc3B1ciIsImlhdCI6MTc1ODg3OTU0NSwiZXhwIjoxNzU4OTA4MzQ1fQ.cs12PtekDO_rIV0-qn7mKdelzwJ3vnNW8Z_FzjTw98RDCEu1Q4gWil-_ye2ZQPKdi3NmH6VftZBTQ2IicgLb6g; userInfoKey={%22role%22:%22ROLE_AREA%22%2C%22isInitialPassword%22:%220%22%2C%22regionName%22:%22%E8%B4%B5%E5%B7%9E%E7%9C%81%22%2C%22appCode%22:%22dgov-portal%22%2C%22organName%22:%22%E6%96%B0%E8%BF%9B%E4%BA%BA%E5%91%98%22%2C%22regionCode%22:%22520000%22%2C%22phone%22:%2204b6bf9c6e9dd2555b6a4771cdc3e7ba%22%2C%22organCode%22:%2252000020170216100530000507%22%2C%22expirationTime%22:86%2C%22name%22:%226e7c1d2bcac5e280ca91a05b5b60cd18%22%2C%22id%22:%221D1D112A50C144A5A0741E56E54BC9F0%22%2C%22email%22:%22d07485450a4b010167a8407b3a713857%22%2C%22username%22:%22187c7c36c1a80ae5bb7963b17986f6d7%22%2C%22status%22:%221%22%2C%22isWeakPwd%22:%220%22}; pwdState=true; inspurRoleKey=ROLE_AREA; windowId=0B7CA12E92004E0ABCA9EB88F0A792E4; James_cookie_sYQDUGqqzHtheme=custom; SameSite=None',
    'Priority': 'u=0',
}
params = {
    'pageFlag': 'enterBusinessQuery',
}

data={
  "_search": "true",
  "nd": int(datetime.now().timestamp() * 1000),
  "rows": "3000",
  "page": "1",
  "sidx": "SUBMIT_TIME",
  "sord": "desc",
  "itemname": "",
  "currentNodeName": "",
  "receiveNumber": "",
  "start": "",
  "end": "",
  "start_time": "",
  "end_time": "",
  "days": "",
  "restDays": "",
  "apply_from": "",
  "itemType": "",
  "agentCode": "",
  "subType": "",
  "latestOrg": "0",
  "businessObjectType": "",
  "applicant": "",
  "iorg": "",
  "state": "",
  "statetype": "1",
  "apply_subject": "",
  "accept_user_name": "",
  "user_name": "",
  "whetherPromise": ""
}

response = requests.post(
    'https://gzmh.zwfw.guizhou.gov.cn/tysp/approval/worktodolist/businessQuery',
    params=params,
    headers=headers,
    cookies=cookies,
    json=data
)
print(response.text)
list=[]
for i in response.json()['rows']:
    print(i['cell'])
    RECEIVE_TIME=datetime.fromtimestamp(int(i['cell']['RECEIVE_TIME'])/1000).strftime("%Y-%m-%d %H:%M:%S")
    SUBMIT_TIME=datetime.fromtimestamp(int(i['cell']['SUBMIT_TIME'])/1000).strftime("%Y-%m-%d %H:%M:%S")
    TIME_LIMIT=datetime.fromtimestamp(int(i['cell']['TIME_LIMIT'])/1000).strftime("%Y-%m-%d %H:%M:%S")
    list.append([i['cell']['RECEIVE_NUMBER'],i['cell']['APPLY_SUBJECT']
                 ,SUBMIT_TIME,i['cell']['ACCEPT_USER_NAME'],i['cell']['ORG_NAME'],
                 i['cell']['REGION_NAME'],i['cell']['USER_NAME'],TIME_LIMIT,i['cell']['RESTDAYS'],
                 i['cell']['COSTHOURS'],i['cell']['INVALID_NODE_NAME']
                 ])


# 表头（按需求排序）
headers = [
    '申办编号', '业务主题', '受理时间', '受理人', '受理单位',
    '行政区划', '当前办理人', '承诺时间', '剩余工作日',
    '审批用时', '当前环节'
]

# 截取数据中与表头对应的部分（取前11个字段，与表头匹配）
formatted_data = [row[:11] for row in list]

# 创建DataFrame
df = pd.DataFrame(formatted_data, columns=headers)

# 写入Excel（需安装openpyxl库）
df.to_excel('业务数据.xlsx', index=False, engine='openpyxl')

print("数据已成功写入业务数据.xlsx")